package com.ymt.bpm.web.rest.engine.taskcenter;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PTaskTodo;
import com.ymt.bpm.service.engine.taskcenter.TaskService;
import com.ymt.bpm.util.AES;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

/**
 * Created by Johnny on 2017/4/11.
 */
@RestController
@RequestMapping("/api/v1/engine/taskcenter/task")
public class TaskResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TaskService ts;

    @GetMapping("draftToTemplate/{tiId}")
    public JSONObject draftToTemplate(HttpServletRequest req,
                                  @PathVariable("tiId")String tiId,
                                  @RequestParam(value="title")String title) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        try {
            title = URLDecoder.decode(new String(Base64Utils.decodeFromString(title), Const.ENCODING), Const.ENCODING);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (log.isDebugEnabled()) {
            log.debug("draftToTemplate tenantId"+tenantId);
            log.debug("draftToTemplate loginName"+loginName);
            log.debug("draftToTemplate tiId"+tiId);
            log.debug("draftToTemplate title"+title);
        }
        return ts.draftToTemplate(tenantId, loginName, tiId, title);
    }

    @DeleteMapping("draft/{tiId}")
    public JSONObject deleteDraft(HttpServletRequest req,
                                  @PathVariable("tiId")String tiId,
                                  @RequestParam("piId")String piId,
                                  @RequestParam(value="purpose", defaultValue = "2")int purpose) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        if (log.isDebugEnabled()) {
            log.debug("deleteDraft tenantId"+tenantId);
            log.debug("deleteDraft loginName"+loginName);
            log.debug("deleteDraft purpose"+purpose);
            log.debug("deleteDraft tiId"+tiId);
            log.debug("deleteDraft piId"+piId);
        }
        return ts.deleteDraft(tenantId, loginName, purpose, tiId, piId);
    }

    @DeleteMapping("cleanDraft")
    public JSONObject deleteDraft(HttpServletRequest req,
                                  @RequestParam(value="purpose", defaultValue = "2")int purpose) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        if (log.isDebugEnabled()) {
            log.debug("deleteDraft tenantId"+tenantId);
            log.debug("deleteDraft loginName"+loginName);
            log.debug("deleteDraft purpose"+purpose);
        }
        return ts.cleanDraft(tenantId, loginName, purpose);
    }

}
